#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;

typedef void(*work_t)(int);
typedef void(*task_t)(int,int);

void task1(int pipecode, int subpid)
{
    cout << "task1" << " pipecode->" << pipecode << " subpid->" << subpid << endl;
}

void task2(int pipecode, int subpid)
{
    cout << "task2" << " pipecode->" << pipecode << " subpid->" << subpid << endl;
}

void task3(int pipecode, int subpid)
{
    cout << "task3" << " pipecode->" << pipecode << " subpid->" << subpid << endl;
}

task_t tasks[3] = {task1, task2, task3};

void worker(int pipecode)
{
    while(1)
    {
        int work_code = 0;
        int read_return_value = read(0, &work_code, sizeof(work_code));

        if(read_return_value == sizeof(work_code))
        {
            if(work_code >= 3) continue;
            tasks[work_code](pipecode,getpid());
        }
        else if(read_return_value == 0)
        {
            break;
        }
        else
        {
            break;
        }

        sleep(1);
    }
}